<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pro_perlengkapan_teknisi extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('Pro_perlengkapan_teknisi_model', 'PerlengkapanTeknisi');
		$this->secure_session();
	}

	function index() {
		$data['title'] = "List Perlengkapan Teknisi";
		$data['it_app']=$this->PerlengkapanTeknisi->GetApp();
		viewByTemplate('pro_perlengkapan_teknisi/index', $data, 'grid');
	}

	function getdata() {
		$this->secure_session();
		echo $this->PerlengkapanTeknisi->GetJSON();
		
	}

	function id_cabang(){
		$nama_cabang = $this->input->post('option');
		$query = $this->db->query("SELECT id_cabang FROM pro_setup_cabang WHERE nama_cabang='".$nama_cabang."'");
		$hasil = $query->row_array();
		$data['id_cabang']=$hasil['id_cabang'];
		echo json_encode($data);
	}

	function nama_cabang(){
		//$nama_cabang = $this->input->post('option');
		$query = $this->db->query("	SELECT	nama_cabang
									FROM	ADMIN_PRIMKOKAS.PRO_SETUP_CABANG
									ORDER BY id_cabang ASC");
		$hasil = $query->result_array();

		echo json_encode($hasil);
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Perlengkapan Teknisi";
		$data['data'] = $this->PerlengkapanTeknisi->GetSingleView(get('id_perangkat'));
		$id_perangkat = get('id_perangkat');
		$data['id'] = $id_perangkat;

		$query2 = $this->db->query("SELECT jumlah,jumlah_pakai FROM pro_barang WHERE id_perangkat='".$id_perangkat."'");
		$getquery2 = $query2->row_array();

		$data['jum_pakai'] = $getquery2['jumlah_pakai'];
		$data['jum_awal'] = $getquery2['jumlah'];
		//var_dump($data['jum_awal']);
		
		$data['id_cabang'] = get('id_cabang');
		$data['nama_cabang'] = get('nama_cabang');
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_perlengkapan_teknisi/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Master Perangkat";
		$data['data'] = $this->PerlengkapanTeknisi->GetSingleView(get('kode_barang'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_perlengkapan_teknisi/form', $data, 'form');
	}

	function show()
	{
		// Display Form View
		$data['title'] = "View Data Master Perangkat";
		$data['data'] = $this->PerlengkapanTeknisi->GetSingleView(get('kode_barang'));
		
		$data['mode'] = "show";
		viewByTemplate('pro_perlengkapan_teknisi/form', $data, 'form');
	}

	function ajax_post() {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {

				// Fill Post Data
				$data = array(
							'kode_perangkat' => $this->input->post('kode_perangkat'),
							'kode_barang' => $this->input->post('kode_barang'),
							'id_perangkat' => $this->input->post('id_perangkat'),
							'nama_barang' => $this->input->post('nama_barang'),
							'nama_merek' => $this->input->post('nama_merek'),
							'tipe' => $this->input->post('tipe'),
							'harga' => $this->input->post('harga'),
							'spesifikasi' => $this->input->post('spesifikasi'),
							'kondisi' => $this->input->post('kondisi'),
							//'jumlah' => $this->input->post('jumlah'),
							'harga' => $this->input->post('harga'),
							'jumlah' => $this->input->post('update_stock'),
							'jumlah_pakai' => $this->input->post('jumlah_pakai'),
							'kondisi' => $this->input->post('kondisi'),
							'keterangan' => $this->input->post('keterangan'),
							'jenis_kebutuhan' => $this->input->post('jenis_kebutuhan'),
							'nama_cabang' => $this->input->post('nama_cabang'),
							'id_cabang' => $this->input->post('id_cabang'),
							);

				if (post("mode") == "create") {
					$this->PerlengkapanTeknisi->Add($data);

					$ls_response = "Data Tersimpan";

				} else if (post("mode") == "edit") {
					$this->PerlengkapanTeknisi->Update($data, 
										array('id_perangkat'	=> $data['id_perangkat']));

					$ls_response = "Data Tersimpan";

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("id_perangkat") == '') {
			$error->Add("id_perangkat", "id_perangkat", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM ADMIN_PRIMKOKAS.PRO_BARANG WHERE id_perangkat = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_perangkat"))))."'")) > 0) {
			$error->Add("id_perangkat", "id_perangkat", $tpl_duplicate);
		}

		return $error;
	}

	function ajax_delete() {
		$ls_result	= "";
		$no	= get('id_perangkat');
		if ($no != "") {
			// Delete Single
			$arr_rule	= array('id_perangkat' => $no);
			$ls_result = $this->PerlengkapanTeknisi->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_perangkat' => $row['id_perangkat']);
				$ls_result .= $this->PerlengkapanTeknisi->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

	/*function cetak(){
		$cost_centre	= get('cost_centre');
		
		if($cost_centre!='' && $cost_centre!=''){
			
			// Load FPDF
			$this->load->helper('fpdf');
			
			// Set Data
			$data['cost_centre'] = $this->PerlengkapanTeknisi->GetSingleView($cost_centre);
			//$data['arr_detail']	= $this->Detail->GetDetailByID($no_po);
			
			// Load View
			$this->load->view('mis_permintaan_um/cetak', $data);
			
		}
	}*/

}
?>